package com.yunkaweilai.android.activity.member;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.classic.common.MultipleStatusView;
import com.github.lazylibrary.util.StringUtils;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.member.MemberGoodsInfoModel;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.model.member.RemainingInfoModel;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.yunkaweilai.android.view.popup.ImageEnlagePopup;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 剩余次数查询，计次计时，包时段
 */
public class RemainingTimesActivity extends BaseActivity {

    public static final String REMAINING_TYPE = "REMAINING_TYPE";
    public static final String REMAINING_MEMBER_MODEL = "REMAINING_MEMBER_MODEL";
    public static final String REMAINING_MEMBER_BUNDLE = "REMAINING_MEMBER_BUNDLE";
    public static final String COUNT_TYPE = "COUNT_TYPE";//计次
    public static final String TIME_TYPE = "TIME_TYPE";//计时
    public static final String MORE_TIME_TYPE = "MORE_TIME_TYPE";//包时段
    @BindView(R.id.content_view)
    ListView idListview;
    @BindView(R.id.id_img_head)
    ImageView idImgHead;
    @BindView(R.id.id_tv_name)
    TextView idTvName;
    @BindView(R.id.id_tvcard_number)
    TextView idTvcardNumber;
    @BindView(R.id.id_tv_card_infor)
    TextView idTvCardInfor;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    private ArrayList<RemainingInfoModel> list = new ArrayList<>();
    private CommonAdapter<RemainingInfoModel> adapter;
    private MemberHeadModel model;
    private String type;
    private MemberGoodsInfoModel memberGoodsInfoModel;

    public static void actionStart(Context context, String type, MemberHeadModel model) {
        Intent intent = new Intent(context, RemainingTimesActivity.class);
        intent.putExtra(REMAINING_TYPE, type);
        Bundle bundle = new Bundle();
        bundle.putSerializable(REMAINING_MEMBER_MODEL, model);
        intent.putExtra(REMAINING_MEMBER_BUNDLE, bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_remaining_times);
        Intent intent = getIntent();
        type = intent.getStringExtra(REMAINING_TYPE);
        model = (MemberHeadModel) intent.getBundleExtra(REMAINING_MEMBER_BUNDLE).getSerializable(REMAINING_MEMBER_MODEL);
        ButterKnife.bind(this);
        initadapter();
        initHeadView();
        initHttp();
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initClick();
    }

    private void initClick() {
        idImgHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utils.ImageEnlage(mAc,"1",model.getMember_avatar(),model.getMember_sex());
            }
        });
    }

    private void initHeadView() {
        String titles = "";
        if (type.equals(COUNT_TYPE)){
            titles = "剩余计次";
        }else if (type.equals(TIME_TYPE)) {
            titles = "剩余计时";
        }else if (type.equals(MORE_TIME_TYPE)){
            titles = "剩余包时段";
        }else{
            titles = "剩余次数";
        }
        new TitleBuilder(mAc).setTitleText(titles).setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Utils.MemberHeadImg(mAc,model.getMember_avatar(),model.getMember_sex(),idImgHead);

        idTvName.setText(model.getMember_name() + " (" + model.getLevel_name() + ": " + model.getCard_no() + ")");
        idTvCardInfor.setText("折扣：" + model.getDiscount()/10  + "折   积分：" + model.getMember_points() + "   余额：" + model.getMember_price());

    }

    private void initHttp() {
        HttpRequest.postUrl(Api.MEMBER_SURPLUS_GOODS_INFO)
                .addParams("member_id", model.getId())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        Gson gson = new Gson();
                        if (checkCode) {
                            memberGoodsInfoModel = gson.fromJson(json, MemberGoodsInfoModel.class);
                            if (memberGoodsInfoModel!=null){
                                initView();
                            }else{
                                if (idMultipleStatusView != null) {
                                    idMultipleStatusView.showError();
                                }
                            }
                        } else {
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }

    private void initView() {
        if (type.equals(COUNT_TYPE)){
            list.clear();
            List<MemberGoodsInfoModel.DataBean.JiciGoodsBean> jiciGoodsList = memberGoodsInfoModel.getData().getJiciGoods();
            for (int i = 0; i < jiciGoodsList.size(); i++) {
                RemainingInfoModel infoModel = new RemainingInfoModel();
                infoModel.setLeft_1("计次项目");
                infoModel.setRight_1(jiciGoodsList.get(i).getGoods_name());
                infoModel.setLeft_2("剩余次数");
                infoModel.setRight_2(jiciGoodsList.get(i).getCount());
                infoModel.setLeft_3("有效期 ");
                infoModel.setRight_3(jiciGoodsList.get(i).getValidity_content());
                infoModel.setType(COUNT_TYPE);
                list.add(infoModel);
            }
            if (jiciGoodsList.size() > 0){
                idMultipleStatusView.showContent();
            }else{
                idMultipleStatusView.showEmpty();
            }

        }else if (type.equals(TIME_TYPE)){
            list.clear();
            List<MemberGoodsInfoModel.DataBean.JishiGoodsBean> jishiGoodsList = memberGoodsInfoModel.getData().getJishiGoods();
            for (int i = 0; i < jishiGoodsList.size(); i++) {
                RemainingInfoModel infoModel = new RemainingInfoModel();
                infoModel.setLeft_1("计时项目");
                infoModel.setRight_1(jishiGoodsList.get(i).getGoods_name());
                infoModel.setLeft_2("");
                infoModel.setRight_2("");
                infoModel.setLeft_3("剩余时间 ");
                infoModel.setRight_3(jishiGoodsList.get(i).getMins() + "分钟");
                infoModel.setType(TIME_TYPE);
                list.add(infoModel);
            }
            if (jishiGoodsList.size() > 0){
                idMultipleStatusView.showContent();
            }else{
                idMultipleStatusView.showEmpty();
            }

        }else if (type.equals(MORE_TIME_TYPE)){
            list.clear();
            List<MemberGoodsInfoModel.DataBean.BaoshiduanGoodsBean> baoshiduanGoodsList = memberGoodsInfoModel.getData().getBaoshiduanGoods();
            for (int i = 0; i < baoshiduanGoodsList.size(); i++) {
                RemainingInfoModel infoModel = new RemainingInfoModel();
                infoModel.setLeft_1("包时段项目");
                infoModel.setRight_1(baoshiduanGoodsList.get(i).getGoods_name()+"");
                infoModel.setLeft_2("");
                infoModel.setRight_2("");
                infoModel.setLeft_3("有效时段 ");
                infoModel.setRight_3(baoshiduanGoodsList.get(i).getStart_date() + "至" + baoshiduanGoodsList.get(i).getEnd_date());
                infoModel.setType(MORE_TIME_TYPE);
                list.add(infoModel);
            }
            if (baoshiduanGoodsList.size() > 0){
                idMultipleStatusView.showContent();
            }else{
                idMultipleStatusView.showEmpty();
            }
        }
        adapter.notifyDataSetChanged();
    }

    private void initadapter() {
        adapter = new CommonAdapter<RemainingInfoModel>(mAc, R.layout.item_list_remaining_times, list) {
            @Override
            protected void convert(ViewHolder viewHolder, RemainingInfoModel item, int position) {
                View view = viewHolder.getView(R.id.id_view1);
                LinearLayout layout1 = viewHolder.getView(R.id.id_llayout1);
                switch (item.getType()) {
                    case COUNT_TYPE:
                        view.setVisibility(View.VISIBLE);
                        layout1.setVisibility(View.VISIBLE);
                        break;
                    case TIME_TYPE:
                    case MORE_TIME_TYPE:
                        view.setVisibility(View.GONE);
                        layout1.setVisibility(View.GONE);
                        break;
                }
                viewHolder.setText(R.id.id_tv_left1,item.getLeft_1());
                viewHolder.setText(R.id.id_tv_left2,item.getLeft_2());
                viewHolder.setText(R.id.id_tv_left3,item.getLeft_3());
                if (StringUtils.isEmpty(item.getRight_1())){
                    viewHolder.setText(R.id.id_tv_right1,"");
                }else{
                    viewHolder.setText(R.id.id_tv_right1,item.getRight_1());
                }
                viewHolder.setText(R.id.id_tv_right2,item.getRight_2());
                viewHolder.setText(R.id.id_tv_right3,item.getRight_3());
            }
        };
        idListview.setAdapter(adapter);
    }
}
